getvariable <- function(vname, dataset.name=dsn) {
  
  #   Args: variable name, quoted
  # Checks: 1 If vname is in workspace, is it character and correct length?
  #         2 If vname is not in workspace.
  #           2.1 vname in variable table?
  #           2.2 Vectorized variable file exists?
  #           2.3 If vname is "weight", is it numeric?
  # Result: Assigns vname in workspace
  
  source("Rfunctions.base/getmetadata.R")

  if (exists(vname)) {
    if (vname != "weight" & !is.character(get(vname))) {
      stop("\"", vname, "\" is not character", sep="")
    }
    if (length(get(vname)) != getmetadata()$numrecords) {
      stop("Length of \"", vname, "\" not equal to ", getmetadata()$numrecords)
    }
    if (vname == "weight" & !is.numeric(get(vname))) {
      stop(paste("\"", weight, "\" is not numeric", sep=""))
    }
  } else {
    # vname present in dsn.metadata/variables.txt?
    vtpath <- paste(dataset.name, ".metadata/variables.txt", sep="")
    vt     <- as.matrix(read.table(vtpath, header=TRUE, stringsAsFactors=FALSE))
    if (!is.element(vname, vt[, "vname"])) {
      stop(paste("\"", vname, "\" does not exist in \"", vtpath, "\"", sep=""))
    }
    
    # Vectorized variable file exists in dsn.variables directory?
    vvfname <- paste(vname, ".gz", sep="")  # vvf = vectorized variable file
    vvdname <- paste(dataset.name, ".variables", sep="")  # vv directory
    vvfpath  <- paste(vvdname, "/", vvfname, sep="")
    if (!file.exists(vvfpath)) {
      stop(paste("\"", vvfname, "\" not found in \"", vvdname, "\"", sep=""))
    } else {
      vpath <- paste(dataset.name, ".variables/", vname, ".gz", sep="")
      cat(paste("getvariable: Getting \"", vvfpath, "\" ...\n", sep=""))
      assign(vname, readLines(vvfpath), envir=globalenv())
    }
    
    # vname is "weight" and it is not numeric? Coerce it to numeric
    if (vname == "weight" && !is.numeric(weight)) {
      cat("getvariable: Coercing \"weight\" to numeric ...\n")
      assign("weight", as.numeric(weight), envir=globalenv())
    }
 }
  
# DESIGN DECISION The "else" checks are carried out only if variable is not
# present in workspace. This allows user to define a variable and use it for
# tabulation without entering it into the variable table or creating a
# compressed vectorized variable for it.

}